home *** CD-ROM | disk | FTP | other *** search
- #!/usr/local/bin/perl
- # find-mx - Find all names which list a host as "best-MX"
- #
- # $Id: dnsfind.shar,v 8.2 1996/10/25 17:07:55 vixie Exp $
- # $Source: /proj/src/isc/cvs-1/bind/contrib/misc/dnsfind.shar,v $
- #
- # SYNOPSIS
- # find-mx hub domain [...]
- #
-
- push (@INC, '/src/util/perl');
- require 'dnsfind.pl';
-
- $Prog = "find-mx";
-
- if ($#ARGV <= 0) {
- die "usage: $Prog hub domain [...]\n";
- }
-
- if (! &main (@ARGV)) {
- exit 1;
- }
-
-
- #############################################################################
- # Main Program
- #
- sub main {
- local ($hub) = shift (@_);
- local (@domains) = @_;
-
- &dnsfind (@domains);
-
- undef %mx;
- foreach $zone (keys (%Exchangers)) {
- @exchangers = split ('\|', $Exchangers{$zone});
-
- #
- # If hub is one of the most preferred MX for this zone, then
- # save it
- #
- if (grep ($_ eq $hub, @exchangers)) {
- $mx{$zone} = 1;
- }
- }
-
- #
- # Check if any CNAMEs point to an MX we have selected
- #
- while (1) {
- ($key, $value) = each (%Cnames);
- last if (! length ($key));
-
- if ($mx{$value}) {
- $mx{$key} = 1;
- }
- }
-
-
- #
- # Custom sort our output
- #
- @mx = sort (custom_sort keys (%mx));
-
- #
- # Print it
- #
- foreach $zone (@mx) {
- print ($zone, "\n");
- }
-
- return 1;
- }
-
-
- #############################################################################
- # dnswanted - Called for each record found by dnsfind
- #
- sub dnswanted {
- local ($pref, $exchanger);
- local ($zone_c);
-
- $type =~ tr/[a-z]/[A-Z]/;
-
- if ($type eq 'MX') {
- local ($pref);
- local ($exchanger);
-
- ($pref, $exchanger) = split ('\s+', $value);
- $zone = &cleanhost ($zone);
- $exchanger = &cleanhost ($exchanger);
-
- if (defined ($Lowest_pref{$zone})) {
- if ($pref == $Lowest_pref{$zone}) {
- $Exchangers{$zone} = $Exchangers{$zone} . '|' .
- $exchanger;
- } else {
- if ($pref < $Lowest_pref{$zone}) {
- $Lowest_pref{$zone} = $pref;
- $Exchangers{$zone} = $exchanger;
- }
- }
- } else {
- $Lowest_pref{$zone} = $pref;
- $Exchangers{$zone} = $exchanger;
- }
- } elsif ($type eq 'CNAME') {
- #
- # Save this, in case it points to a selected MX
- #
- $zone_c = &cleanhost ($zone);
- $Cnames{$zone_c} = &cleanhost ($value);
- }
- }
-
-
- #############################################################################
- # custom_sort - Sorts domain names by number of parts in name then alphanum
- #
- sub custom_sort {
- local ($dots_a);
- local ($dots_b);
- local ($cmp);
-
- $dots_a = $a; $dots_a =~ tr/.//dc;
- $dots_b = $b; $dots_b =~ tr/.//dc;
-
- $cmp = $dots_a cmp $dots_b;
- if ($cmp == 0) {
- $cmp = $a cmp $b;
- }
- return $cmp;
- }
-
- #############################################################################
- # cleanhost - Fold case to lower and remove trailing dot if present
- #
- sub cleanhost {
- local ($host) = shift (@_);
-
- $host =~ tr/[A-Z]/[a-z]/;
- $host =~ s/\.$//;
- return ($host);
- }
-